<?php

class User extends CActiveRecord {
    /**
     * The followings are the available columns in table 'www_users':
     * @var integer $id
     * @var string $username
     * @var string $password
     * @var integer $status
     * @var integer $created
     * @var integer $updated
     */

     public function behaviors() {
        return array(
            'CTimestampBehavior' => array(
                'class' => 'zii.behaviors.CTimestampBehavior',
                'createAttribute' => 'created',
                'updateAttribute' => 'updated',
            )
        );
    }

    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{users}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
        return array(
            array('username, password', 'required'),
            array('status, created, updated', 'numerical', 'integerOnly' => true),
            array('username', 'length', 'max' => 20),
            array('password', 'length', 'max' => 64),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
        return array(
            'group' => array(self::BELONGS_TO, 'UserGroup', 'group_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'Id',
            'group_id' => Yii::t('user', 'group_id'),
            'username' => Yii::t('user', 'username'),
            'password' => Yii::t('user', 'password'),
            'status' => Yii::t('user', 'status'),
            'created' => Yii::t('user', 'created'),
            'updated' => Yii::t('user', 'updated'),
        );
    }

    public $status = 0;
    const STATUS_DISABLED = 0;
    const STATUS_ENABLED = 1;
    public function getStatusOptions() {
        return array(
            self::STATUS_DISABLED => Yii::t('user', 'status_disabled'),
            self::STATUS_ENABLED  => Yii::t('user', 'status_enabled'),
        );
    }

    // "status" field Icon format
    public function getStatusIconFormat() {
        switch ($this->status) {
            case self::STATUS_DISABLED:
                $img = 'icon_disabled.png';
                break;
            case self::STATUS_ENABLED:
                $img = 'icon_enabled.png';
                break;
        }
        return CHtml::image(Yii::app()->assetManager->baseUrl . '/backend/images/icon/' . $img);
    }

    // status AJax Link
    public function getStatusAjaxLink() {
        return CHtml::ajaxLink($this->statusIconFormat,
            Yii::app()->createUrl('backend/users/changeStatus'),
            array(
                'type' => 'POST',
                'data' => 'id=' . $this->id,
                'dataType' => 'text',
                'update' => "#yuandot{$this->id}",
            ),
            array('id' => "yuandot{$this->id}")
        );
    }

    public function getGroupOptions() {
        return UserGroup::model()->findAll();
    }


    // "updated" field formatted
    public function getUpdatedFormatted() {
        return ($this->updated) ? date('Y-m-d', $this->updated) : '';
    }

    // 删除指定用户的同时删除权限分配数据
    public function afterDelete() {
//        $tmp = Assignments::model()->findAll("user_id = {$this->id}");
//        var_dump($tmp);

    }
}